﻿using System;
using System.IO;
using NHibernate.Tool.hbm2ddl;
using WorkLogger.Client.DataAccess;

namespace WorkLogger.Client {
	public static class Deployer {
		public static void Deploy(string workLoggerPath) {
			AppOptions.FileName = Path.Combine(workLoggerPath, "options.xml");
			AppOptions.ProgramPath = workLoggerPath;
			NHibernateHelper.DatabasePath = Path.Combine(workLoggerPath, "work.db");

			if (Directory.Exists(workLoggerPath) == false) {
				Directory.CreateDirectory(workLoggerPath);
			}

			if (File.Exists(NHibernateHelper.DatabasePath) == false) {
				var schemaUpdate = new SchemaUpdate(NHibernateHelper.Configuration);
				schemaUpdate.Execute(Console.WriteLine, true);
			}
			if (File.Exists(AppOptions.FileName) == false) {
				AppOptions.Save(CreateWellOptions());
			}
		}

		private static AppOptions CreateWellOptions() {
			return new AppOptions {
			                      	WorkDayDuration = new TimeSpanXml(new TimeSpan(8, 0, 0)),
			                      	CoffeeBreak = 10,
			                      	DeductIncomeTax = true,
			                      	Salary = 100
			                      };
		}
	}
}